package com.bzkj.web.controller.yxl;

import com.bzkj.basicinfo.domain.basic.vo.WarehouseExportVo;
import com.bzkj.basicinfo.domain.yxl.YxlBillsInDetailList;
import com.bzkj.basicinfo.domain.yxl.YxlBillsList;
import com.bzkj.basicinfo.domain.yxl.YxlPrintDto;
import com.bzkj.basicinfo.domain.yxl.vo.YxlBillsListVo;
import com.bzkj.basicinfo.domain.yxl.vo.YxlExportBillsListVo;
import com.bzkj.basicinfo.service.yxl.YxlBillsService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.DataProcessCommonUtils;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.*;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @Author：swj
 * @Desc
 * @Date：2025/7/31 9:03
 */
@RestController
@RequestMapping("/yxl/bill")
public class YxlBillsController extends BaseController {

    @Autowired
    private YxlBillsService yxlBillsService;

    @ApiOperation("查询接口")
//    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/queryOutBillList")
    public TableDataInfo queryBasicInfo(YxlBillsList entity) {
        startPage();
        return getDataTable(yxlBillsService.selectBillsOutList(entity));
    }
    @GetMapping("/selectBillsListByBillsId")
    public AjaxResult selectBillsOutListByBillsId(String billsId) {
        return yxlBillsService.selectBillsOutListByBillsId(billsId);
    }
    @GetMapping("/selectInBillsListByBillsId")
    public AjaxResult selectInBillsListByBillsId(String billsId) {
        return yxlBillsService.selectBillsInListByBillsId(billsId);
    }

    @ApiOperation("查询接口")
//    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/selectBillsList")
    public TableDataInfo selectOutBillsList(YxlPrintDto dto) {
        if (dto == null) {
            dto = new YxlPrintDto();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return yxlBillsService.queryBillsOutList(dto,pageNum,pageSize);
    }

    @ApiOperation("查询接口")
    @GetMapping("/selectBillsDetailList")
    public AjaxResult selectOutDetailBillsList(YxlPrintDto dto,Integer pageNum,Integer pageSize) {
        return AjaxResult.success(yxlBillsService.queryBillsOutDetailList(dto, pageNum, pageSize));
    }

    @ApiOperation("查询接口")
//    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/queryInBillList")
    public TableDataInfo queryStoStockInfo(YxlBillsList entity) {
        startPage();
        return getDataTable(yxlBillsService.selectBillsInList(entity));
    }
    @ApiOperation("查询接口")
//    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/queryInBillDetail")
    public TableDataInfo queryInBillsDetail(YxlPrintDto dto) {
        startPage();
        return getDataTable(yxlBillsService.selectBillsInDetailList(dto));
    }

    @ApiOperation("打印拼箱")
    @PostMapping("/printSpell")
    public AjaxResult printSpell(@RequestBody YxlPrintDto dto) {
        return yxlBillsService.printSpell(dto);
    }

    @ApiOperation("退回")
    @PostMapping("/rejectBill")
    private AjaxResult rejectBill(@RequestBody YxlPrintDto dto) {
        return yxlBillsService.rejectBill(dto);
    }
    //TODO 出库单据汇总导出

    @ApiOperation("导出出库汇总接口")
    @LogC(title = "出库汇总",businessType = BusinessType.EXPORT)
    @PostMapping("/exportBillsDetailList")
    public AjaxResult exportBillsDetailList(YxlPrintDto dto,@RequestParam String selectedColumns,
                                            HttpServletResponse response) {
        if (dto == null) {
            dto = new YxlPrintDto();
        }
        List<YxlExportBillsListVo> billsLists = yxlBillsService.exportBillsOutList(dto);
        String[] allColumns = getAllColumns(YxlExportBillsListVo.class);
        // 将 selectedColumns 转换为 Set，以便更快地检查是否包含某个属性
        String[] split = selectedColumns.split(",");
        Set<String> selectedSet = new HashSet<>(Arrays.asList(split));
        // 使用列表来存储未选择的列
        List<String> remainingColumns = new ArrayList<>();
        // 遍历所有列，并将未选择的列添加到 remainingColumns 列表中
        for (String column : allColumns) {
            if (!selectedSet.contains(column)) {
                remainingColumns.add(column);
            }
        }
        // 将 remainingColumns 转换回数组
        String[] finalColumns = remainingColumns.toArray(new String[0]);
        ExcelUtil<YxlExportBillsListVo> util = new ExcelUtil<>(YxlExportBillsListVo.class);
        util.hideColumn(finalColumns);
        util.exportExcel(response, billsLists, "出库汇总", "出库汇总", "出库汇总");
        return AjaxResult.success();
    }
    // 获取指定类的所有属性名
    private String[] getAllColumns(Class<?> clazz) {
        List<String> columns = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            columns.add(field.getName());
        }
        return columns.toArray(new String[0]);
    }

    @ApiOperation("查询接口")
    @GetMapping("/queryPdaBillsCheckList")
    public AjaxResult queryPdaBillsCheck(String stoId, String startDate, String endDate, String userName,String billsMode,String billsId
            , Integer pageNum, Integer pageSize) {
        return yxlBillsService.queryPdaBillsCheck(stoId, startDate, endDate, userName, billsMode,billsId,pageNum, pageSize);
    }
    @ApiOperation("查询接口")
    @GetMapping("/queryErpJiXing")
    public AjaxResult queryErpJiXing() {
        return yxlBillsService.queryErpJiXing();
    }

    @ApiOperation("查询接口")
    @GetMapping("/queryJiXingMap")
    public AjaxResult queryJiXingMapList(String areaId) {
        return yxlBillsService.queryJiXingMapList(areaId);
    }

    @ApiOperation("取消确认")
//    @LogC(title = "单据取消确认", businessType = BusinessType.UPDATE, pkFieldNames = "id",tableNames = "PDA_Bills_Check")
    @PostMapping("/cancelCheck")
    @LogC(title = "取消确认", businessType = BusinessType.OTHER,
            tableNames = "PDA_Bills_Check", pkFieldNames = "ID", content = "' 单据号：'+#billsId")
    public AjaxResult cancelCheck(@RequestParam String id, @RequestParam String billsId) {
        return yxlBillsService.cancelCheckById(id,billsId);
    }

    @ApiOperation("查询接口")
    @GetMapping("/queryErpReceipt")
    public AjaxResult queryErpReceipt(String billsId,String ywy,String startDate,String endDate,Integer pageNum,Integer pageSize) {
        return yxlBillsService.queryErpReceipt(billsId,ywy,startDate,endDate,pageNum,pageSize);
    }

    @ApiOperation("查询接口")
    @GetMapping("/queryErpReceiptDetail")
    public AjaxResult queryErpReceiptDetail(String billsId) {
        return yxlBillsService.queryErpReceiptDetail(billsId);
    }

    @ApiOperation("查询接口")
    @GetMapping("/queryWmsReceiptLog")
    public AjaxResult queryWmsReceiptLog(String billsID,String drugId) {
        return yxlBillsService.queryWmsReceiptLog(billsID,drugId);
    }
    @ApiOperation("查询接口")
    @GetMapping("/queryWmsReceived")
    public AjaxResult queryWmsReceived(String billsID,String drugName,String customer,String startDate,String endDate,
                                       Integer pageNum,Integer pageSize) {
        return yxlBillsService.queryWmsReceived(billsID,drugName,customer,startDate,endDate,pageNum,pageSize);
    }

    @ApiOperation("批量收货接口")
    @PostMapping("/confirmReceipt")
    @LogC(title = "批量收货", businessType = BusinessType.INSERT,
            tableNames = "PDA_WareHouseIn_Bills_List_Receipt", pkFieldNames = "ID", content = "' 单据号：'+#billsId")
    public AjaxResult confirmReceipt(@RequestParam String billsId) {
        return yxlBillsService.confirmReceipt(billsId);
    }

    @ApiOperation("单品收货接口")
    @PostMapping("/confirmReceiptBySpid")
    @LogC(title = "单品收货", businessType = BusinessType.INSERT,
            tableNames = "PDA_WareHouseIn_Bills_List_Receipt", pkFieldNames = "ID", content = "' 单据号：'+#billsId+' 商品id：'+#spid")
    public AjaxResult confirmReceiptBySpid(@RequestParam String billsId,@RequestParam String spid,
                                           @RequestParam String lingsshl,@RequestParam String dj) {
        return yxlBillsService.confirmReceiptBySpid(billsId,spid,lingsshl,dj);
    }
    @ApiOperation("拒收接口")
    @PostMapping("/rejectReceiver")
    @LogC(title = "拒收", businessType = BusinessType.INSERT,
            tableNames = "PDA_WareHouseReceipt_Bills_List", pkFieldNames = "ID", content = "' 单据号：'+#billsId+' 商品编号：'+#spid+ ' 拒收原因：'+#remark")
    public AjaxResult rejectReceiver(@RequestParam String billsId,@RequestParam String spid,
                                     @RequestParam String lingsshl,@RequestParam String dj,@RequestParam String remark) {
        return yxlBillsService.rejectReceiver(billsId,spid,lingsshl,dj,remark);
    }

}
